package com.droidathletics.boundary.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.droidathletics.entity.TypeTraining;

public class MicrocycleDAOAdapter {
	private static final String DATABASE_NAME = "droidathleticsdb.db";
	private static final int DATABASE_VERSION = 1;
	private static final String MICROCYCLE_TABLE_NAME = "microcycles";
	
    private static final String MICROCYCLE_TABLE_CREATE =
        "CREATE TABLE " + MICROCYCLE_TABLE_NAME + " (macrocycle TEXT, numberofweek INTEGER, " +
        		"period TEXT, stage TEXT, microcycletype TEXT, " +
        		"objective TEXT, numberhoursperweek INTEGER, analacvelocidad INTEGER, " +
        		"anlacresistveloc INTEGER, anaaerobiomixto INTEGER, aerobico INTEGER, " +
        		"fuerzamaxima INTEGER, fuerzarapida INTEGER, fuerzaresistencia INTEGER, " +
        		"flexibilidad INTEGER, capcoordinativas INTEGER, tecnica INTEGER, " +
        		"tecnicatactica INTEGER, psicologica INTEGER, pbiologica INTEGER, pteorica INTEGER, " +
        		"competencias INTEGER, controles INTEGER, PRIMARY KEY(macrocycle, numberofweek))";
	
    private SQLiteDatabase database;
	private final Context context;
    private MicrocycleDBOpenHelper microcycleHelper;
    
	public MicrocycleDAOAdapter(Context context) {
		this.context = context;
		microcycleHelper = new MicrocycleDBOpenHelper(this.context);
	    this.database = microcycleHelper.getWritableDatabase();
	}
	
	public static class MicrocycleDBOpenHelper extends SQLiteOpenHelper {
		
		public MicrocycleDBOpenHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(MICROCYCLE_TABLE_CREATE);	
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + MICROCYCLE_TABLE_NAME);
			onCreate(db);	
		}
	}

    public void close() {
        microcycleHelper.close();
        database.close();
    }
    
    public void deleteTable()
    {
    	this.database.delete(MICROCYCLE_TABLE_NAME, null, null);
    }
    
    public long createMicrocycle(String macrocycleName,	
		 String numberOfWeek, String period, String stage, String microCycleType,
		 String objective, String numberOfHoursPerWeek,
		 ArrayList<TypeTraining> trainings) {
  
    	long result = -1;
    	ContentValues values = new ContentValues();
    	values.put("macrocycle", macrocycleName);
        values.put("numberofweek", Integer.parseInt(numberOfWeek));
        values.put("period", period);
        values.put("stage", stage);
        values.put("microcycletype", microCycleType);
        values.put("objective", objective);
        values.put("numberhoursperweek", Integer.parseInt(numberOfHoursPerWeek));

        int indexTraining = 0;
        String[] trainingValues = new String[] { "analacvelocidad",
        		"anlacresistveloc", "anaaerobiomixto", "aerobico", "fuerzamaxima", "fuerzarapida", 
        		"fuerzaresistencia", "flexibilidad", "capcoordinativas", "tecnica",
        		"tecnicatactica", "psicologica", "pbiologica", "pteorica", "competencias", "controles"};
        
        for ( TypeTraining t : trainings)
        {
        	if ( t.getName() == null )
        	{
        	  break;
        	}
        	values.put(trainingValues[indexTraining], t.getVolume());
        	indexTraining++;
        }
        
        
        try {
            result = database.insertOrThrow(MICROCYCLE_TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }

			
    public long updateMicrocycle(int numberOfWeek, String macrocycleName,			
		 String numberWeek, String period, String stage, String microCycleType,
		 String objective, String numberOfHoursPerWeek,
		 ArrayList<TypeTraining> trainings) {
        
    	long result = -1;
    	ContentValues values = new ContentValues();
    	values.put("macrocycle", macrocycleName);
        values.put("numberofweek", Integer.parseInt(numberWeek));
        values.put("period", period);
        values.put("stage", stage);
        values.put("microcycletype", microCycleType);
        values.put("objective", objective);
        values.put("numberhoursperweek", Integer.parseInt(numberOfHoursPerWeek));
        
        int indexTraining = 0;
        String[] trainingValues = new String[] { "analacvelocidad",
        		"anlacresistveloc", "anaaerobiomixto", "aerobico", "fuerzamaxima", "fuerzarapida", 
        		"fuerzaresistencia", "flexibilidad", "capcoordinativas", "tecnica",
        		"tecnicatactica", "psicologica", "pbiologica", "pteorica", "competencias", "controles"};
        
        for ( TypeTraining t : trainings)
        {
        	values.put(trainingValues[indexTraining], t.getVolume());
        	indexTraining++;
        }        
        
        try {
            result = database.update(MICROCYCLE_TABLE_NAME, values, 
            		      "numberofweek='"+numberOfWeek+"' AND macrocycle='"+macrocycleName+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long deleteMicrocycle(int numberWeek, String macrocycleName){
        long result = -1;
        
    	try {
            result = database.delete(MICROCYCLE_TABLE_NAME, 
            		                 "numberofweek='"+numberWeek+"' AND macrocycle='"+macrocycleName+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    	
    }

   public Cursor readMicrocycle(){
	   return database.query(MICROCYCLE_TABLE_NAME, new String[] {
				"macrocycle", "numberofweek", "period", "stage", "microcycletype", "objective",
				"numberhoursperweek", "analacvelocidad",
        		"anlacresistveloc", "anaaerobiomixto", "aerobico", "fuerzamaxima", "fuerzarapida", 
        		"fuerzaresistencia", "flexibilidad", "capcoordinativas", "tecnica",
        		"tecnicatactica", "psicologica", "pbiologica", "pteorica", "competencias", "controles"}, null, null, null,
				null, null);
   
   }
   
   public Cursor readMicrocycle(String macrocycleName){
	   
	   Cursor cursor = null;
	   try{
		   cursor = database.query(MICROCYCLE_TABLE_NAME, new String[] {
					"macrocycle", "numberofweek", "period", "stage", "microcycletype", "objective",
					"numberhoursperweek", "analacvelocidad",
	        		"anlacresistveloc", "anaaerobiomixto", "aerobico", "fuerzamaxima", "fuerzarapida", 
	        		"fuerzaresistencia", "flexibilidad", "capcoordinativas", "tecnica",
	        		"tecnicatactica", "psicologica", "pbiologica", "pteorica", "competencias", "controles"}, "macrocycle='"+macrocycleName+"'", null, null,
					null, null);  

	   } catch(Exception e){
		   e.printStackTrace();
	   }
	   
   return cursor;
   }
}
